package com.ky.manage.utils;

import com.getkeepsafe.relinker.ReLinker;
import com.google.gson.Gson;
import com.ky.manage.constant.OverallData;
import com.ky.manage.utils.log.StaticLogUtil;
import com.tencent.mmkv.MMKV;

/**
 * @author kongxingfei
 * @CreateTime 2020/9/29 11:32
 * @Description
 */
public class MMKVUtil {
    private static final String TAG = "MMKVUtil";
    private static MMKVUtil mInstance;
    private MMKV mmkv;

    private MMKVUtil() {
        try {
            //解决apk安装或升级概率遇到libmmkv.so找不到问题
            String dir = OverallData.app.getFilesDir().getAbsolutePath() + "/mmkv";
            MMKV.initialize(dir, libName -> {
                //不用系统的loader加载库
                //使用ReLinker加载库，实现思路：加载so文件时却找不到相关的so文件，
                // Relinker会尝试从apk中拷贝so库到/data/data/myPackage/libs 目录，修改其读写权限。
                //开源库[ReLinker](https://github.com/KeepSafe/ReLinker) 专门解决这个问题
                ReLinker.loadLibrary(OverallData.app , libName);
            });
            mmkv = MMKV.defaultMMKV();
        } catch (Exception e) {
            StaticLogUtil.getInstance().logInfo(TAG, "mmkv初始化失败，e = " + e.toString());
        }
    }

    public static MMKVUtil getInstance() {
        if (mInstance == null) {
            synchronized (MMKVUtil.class) {
                if (mInstance == null) {
                    mInstance = new MMKVUtil();
                }
            }
        }
        return mInstance;
    }

    public void clear() {
        if (mmkv != null) {
            mmkv.clearAll();
        }
    }

    public void removeKey(String key) {
        if (mmkv != null) {
            mmkv.remove(key);
        }
    }

    public <T> T readObjectFromMMKV(String key, Class<T> clazz) {
        if (mmkv != null) {
            return new Gson().fromJson(mmkv.getString(key, ""), clazz);
        } else {
            return null;
        }
    }

    public <T> void saveObjectToMMKV(String key, T t) {
        if (mmkv != null) {
            mmkv.putString(key, new Gson().toJson(t)).apply();
        }
    }

    public String readStringFromMMKV(String key, String defValue) {
        if (mmkv != null) {
            return mmkv.getString(key, defValue);
        } else {
            return defValue;
        }
    }

    public void saveStringToMMKV(String key, String value) {
        if (mmkv != null) {
            mmkv.putString(key, value).apply();
        }
    }

    public synchronized void saveLongToMMKV(String key, long value) {
        if (mmkv != null) {
            mmkv.putLong(key, value).apply();
        }
    }

    public synchronized long readLongFromMMKV(String key, long defValue) {
        if (mmkv != null) {
            return mmkv.getLong(key, defValue);
        } else {
            return defValue;
        }
    }

    public synchronized int readIntFromMMKV(String key, int defValue) {
        if (mmkv == null) {
            return defValue;
        } else {
            return mmkv.getInt(key, defValue);
        }
    }

    public synchronized void saveIntToMMKV(String key, int value) {
        if (mmkv != null) {
            mmkv.putInt(key, value);
        }
    }

    public synchronized boolean readBooleanFromMMKV(String key, boolean defValue) {
        if (mmkv == null) {
            return defValue;
        } else {
            return mmkv.getBoolean(key, defValue);
        }
    }

    public synchronized void saveBooleanToMMKV(String key, boolean value) {
        if (mmkv != null) {
            mmkv.putBoolean(key, value);
        }
    }


    public synchronized void saveFileIsValid(String key, boolean valid) {
        if (mmkv != null) {
            mmkv.putBoolean(key, valid).apply();
        }
    }


    public synchronized boolean getFileIsValid(String key) {
        if (mmkv != null) {
            return mmkv.getBoolean(key, true);
        } else {
            return true;
        }
    }


}
